package com.cn.algorithm02.class04;

/***
 * @author: hels
 * @description:
 **/
public class C01_ReverseLinkedNode {
    public static void main(String[] args) {
        // 双链表
        NodeDouble headDou = NodeDouble.getHead();
        System.out.println(headDou);
        System.out.println(headDou.next);
        System.out.println(headDou.next.next);

        System.out.println("=====");
        NodeDouble headDou2 = reverseDNOde(headDou);
        System.out.println(headDou2);
        System.out.println(headDou2.next);
        System.out.println(headDou2.next.next);


        // 单链表反转
    /*    Nodea value = Nodea.getHead();
        System.out.println(value);

        Nodea node = reverseNode(value);
        System.out.println(node);*/
    }

    // 双链表
    // ->   null < a = b = c > null
    // ->   null < c = b = a > null
    static NodeDouble reverseDNOde(NodeDouble head) {
        NodeDouble pre = null;
        NodeDouble next = null;
        NodeDouble newHead = null;

        while (head != null) {
            newHead = head;
            next = head.next;
            head.next = pre;
            head.last = next;
            pre = head;
            head = next;
        }

        return pre;
    }

    // a > b > c > null   单链表
    // c > b > a > null  // todo  有问题
    static Node reverseNode(Node head) {
        Node pre = null;
        Node next = null;
        while (head != null) {
           next = head.next;
           head.next = pre;
           pre = head;
           head = next;
        }
        return pre;
    }


    static NodeDouble reverseDouNode(NodeDouble head) {
        NodeDouble pre = null;
        NodeDouble next = null;
        while (head != null) {
            next = head.next; // b
            head.next = pre; // 反转
            head.last = next;
            pre = head;
            head = next;
        }
        return pre;
    }
}
